// app.js
import websocketUtils from '/utils/websocketUtil'
import httpUtil from '/utils/httpUtil'
App({
  onLaunch() {
    var that  = this;
    this.globalData.websocketUtils = new websocketUtils('wss://wss.huisonglin.top/websoket/endpint/1223',function(e){
      // that.globalData.token = e
      // const message = wx.getStorageSync('message') || []
      // message.unshift(e)
      // wx.setStorageSync('message', message)
    })

    if(this.globalData.token == null){
      this.login();
    }


  },
  login(){
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res)
        httpUtil.getRequest('/api/user/login',{
          code:res.code
        }).then(res =>{
            console.log(res)
          this.globalData.token = res.token
          this.globalData.userId = res.userId
        })
      },
      fail: res=>{
        console.log(res)
      }
    })
  },
  //在app.js中定义一个方法 监听globalData的属性isLog变化
  isLoginWatch:function(callBack){
    var obj = this.globalData;
    Object.defineProperty(obj,"token", {
      configurable: true,
      enumerable: true,
      set: function (value) {
        // value就是被新赋予的值，通过回调函数，把这值回调出去
        this._token = value;
        callBack(value);
      },
      get: function(){
        return this._token
      }
    })
  },

  globalData: {
    userInfo: null,
    token: null,
    websocketUtils: null,
  }
  
})
